package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"gopkg.in/yaml.v2"
)

// An example showing how to unmarshal embedded
// structs from YAML.

type StructA struct {
	A string `yaml:"a"`
}

type StructB struct {
	// Embedded structs are not treated as embedded in YAML by default. To do that,
	// add the ",inline" annotation below
	A StructA `yaml:"a"`
	B string  `yaml:"b"`
}

// 配置信息
type Conf struct {
	Server  ServerConf `yaml:"server"`
	AppConf AppConf    `yaml:"app"`
	DbConf  DbConf     `yaml:"db"
`
}

type ServerConf struct {
	Host string `yaml:"host"`
	Port int    `yaml:"port"`
}

type AppConf struct {
	Name string `yaml:"name"`
}

type DbConf struct {
	Host       string `yaml:"host"`
	Port       int    `yaml:"port"`
	Username   string `yaml:"username"`
	Password   string `yaml:"password"`
	Dbname     string `yaml:"dbname"`
	SslMode    string `yaml:"sslMode"`
	Timezone   string `yaml:"timezone"`
	DbPoolConf `yaml:"pool"`
}

type DbPoolConf struct {
	MaxIdleConn int `yaml:"maxIdleConn"`
	MaxOpenConn int `yaml:"maxOpenConn"`
}

func main() {
	var b Conf
	data, _ := ioutil.ReadFile("setting.yaml")
	err := yaml.Unmarshal(data, &b)
	if err != nil {
		log.Fatalf("cannot unmarshal data: %v", err)
	}
	fmt.Println(b)

}
